home *** CD-ROM | disk | FTP | other *** search
/ PC User 2003 January / Disc 3 / Amethyst.iso / live / usr / share / vim / vim60 / plugin / gzip.vim < prev    next >
Encoding:
Text File  |  2002-06-19  |  4.1 KB  |  129 lines

  1. " Vim plugin for editing compressed files.
  2. " Maintainer: Bram Moolenaar <Bram@vim.org>
  3. " Last Change: 2001 Sep 20
  4.  
  5. " Exit quickly when:
  6. " - this plugin was already loaded
  7. " - when 'compatible' is set
  8. " - some autocommands are already taking care of compressed files
  9. if exists("loaded_gzip") || &cp || exists("#BufReadPre#*.gz")
  10.   finish
  11. endif
  12. let loaded_gzip = 1
  13.  
  14. augroup gzip
  15.   " Remove all gzip autocommands
  16.   au!
  17.  
  18.   " Enable editing of gzipped files
  19.   " set binary mode before reading the file
  20.   " use "gzip -d", gunzip isn't always available
  21.   autocmd BufReadPre,FileReadPre    *.gz,*.bz2,*.Z setlocal bin
  22.   autocmd BufReadPost,FileReadPost    *.gz  call s:read("gzip -d")
  23.   autocmd BufReadPost,FileReadPost    *.bz2 call s:read("bzip2 -d")
  24.   autocmd BufReadPost,FileReadPost    *.Z   call s:read("uncompress")
  25.   autocmd BufWritePost,FileWritePost    *.gz  call s:write("gzip")
  26.   autocmd BufWritePost,FileWritePost    *.bz2 call s:write("bzip2")
  27.   autocmd BufWritePost,FileWritePost    *.Z   call s:write("compress -f")
  28.   autocmd FileAppendPre            *.gz  call s:appre("gzip -d")
  29.   autocmd FileAppendPre            *.bz2 call s:appre("bzip2 -d")
  30.   autocmd FileAppendPre            *.Z   call s:appre("uncompress")
  31.   autocmd FileAppendPost        *.gz  call s:write("gzip")
  32.   autocmd FileAppendPost        *.bz2 call s:write("bzip2")
  33.   autocmd FileAppendPost        *.Z   call s:write("compress -f")
  34. augroup END
  35.  
  36. " Function to check that executing "cmd [-f]" works.
  37. " The result is cached in s:have_"cmd" for speed.
  38. fun s:check(cmd)
  39.   let name = substitute(a:cmd, '\(\S*\).*', '\1', '')
  40.   if !exists("s:have_" . name)
  41.     let e = executable(name)
  42.     if e < 0
  43.       let r = system(name . " --version")
  44.       let e = (r !~ "not found" && r != "")
  45.     endif
  46.     exe "let s:have_" . name . "=" . e
  47.   endif
  48.   exe "return s:have_" . name
  49. endfun
  50.  
  51. " After reading compressed file: Uncompress text in buffer with "cmd"
  52. fun s:read(cmd)
  53.   " don't do anything if the cmd is not supported
  54.   if !s:check(a:cmd)
  55.     return
  56.   endif
  57.   " make 'patchmode' empty, we don't want a copy of the written file
  58.   let pm_save = &pm
  59.   set pm=
  60.   " set 'modifiable'
  61.   let ma_save = &ma
  62.   setlocal ma
  63.   " when filtering the whole buffer, it will become empty
  64.   let empty = line("'[") == 1 && line("']") == line("$")
  65.   let tmp = tempname()
  66.   let tmpe = tmp . "." . expand("<afile>:e")
  67.   " write the just read lines to a temp file "'[,']w tmp.gz"
  68.   execute "silent '[,']w " . tmpe
  69.   " uncompress the temp file: call system("gzip -d tmp.gz")
  70.   call system(a:cmd . " " . tmpe)
  71.   " delete the compressed lines
  72.   '[,']d
  73.   " read in the uncompressed lines "'[-1r tmp"
  74.   setlocal nobin
  75.   execute "silent '[-1r " . tmp
  76.   " if buffer became empty, delete trailing blank line
  77.   if empty
  78.     silent $delete
  79.     1
  80.   endif
  81.   " delete the temp file and the used buffers
  82.   call delete(tmp)
  83.   silent! exe "bwipe " . tmp
  84.   silent! exe "bwipe " . tmpe
  85.   let &pm = pm_save
  86.   let &l:ma = ma_save
  87.   " When uncompressed the whole buffer, do autocommands
  88.   if empty
  89.     execute ":silent! doau BufReadPost " . expand("%:r")
  90.   endif
  91. endfun
  92.  
  93. " After writing compressed file: Compress written file with "cmd"
  94. fun s:write(cmd)
  95.   " don't do anything if the cmd is not supported
  96.   if s:check(a:cmd)
  97.     " Rename to a weird name to avoid the risk of overwriting another file
  98.     let nm = expand("<afile>")
  99.     let nmt = expand("<afile>:p:h") . "/X~=@l9q5"
  100.     if rename(nm, nmt) == 0
  101.       call system(a:cmd . " " . nmt)
  102.       call rename(nmt . "." . expand("<afile>:e"), nm)
  103.     endif
  104.   endif
  105. endfun
  106.  
  107. " Before appending to compressed file: Uncompress file with "cmd"
  108. fun s:appre(cmd)
  109.   " don't do anything if the cmd is not supported
  110.   if s:check(a:cmd)
  111.     " Rename to a weird name to avoid the risk of overwriting another file
  112.     let nm = expand("<afile>")
  113.     let nmt = expand("<afile>:p:h") . "/X~=@l9q5"
  114.     let nmte = nmt . "." . expand("<afile>:e")
  115.     if rename(nm, nmte) == 0
  116.       if &patchmode != "" && getfsize(nm . &patchmode) == -1
  117.     " Create patchmode file by creating the decompressed file new
  118.     call system(a:cmd . " -c " . nmte . " > " . nmt)
  119.     call rename(nmte, nm . &patchmode)
  120.       else
  121.     call system(a:cmd . " " . nmte)
  122.       endif
  123.       call rename(nmt, nm)
  124.     endif
  125.   endif
  126. endfun
  127.  
  128. " vim: set sw=2 :
  129.